var xrsdk = require('../../../vendor/hope-sdk/core/request.js');
var config = require('../../../config.js');
var app = getApp();
Page({
  data: {
    lists: [],
    touchmove: false, // 是否在索引表上滑动
    touchmoveIndex: -1,
    titleHeight: 0, // A字距离窗口顶部的高度
    indexBarHeight: 0, // 索引表高度
    indexBarItemHeight: 0, // 索引表子项的高度
    winHeight: 0,
    scrollTop: 0,

    inputShowed: false, // 输入框是否显示
    inputVal: '', // 搜索框输入的内容
    searchResult: [],// 搜索结果
  },
  onLoad: function (options) {
    const that = this;
   // setTimeout(() => {
      wx.getSystemInfo({
        success: function (res) {
          let winHeight = res.windowHeight
          let barHeight = winHeight - res.windowWidth / 750 * 232
          that.setData({
            winHeight: winHeight,
            indexBarHeight: barHeight,
            indexBarItemHeight: barHeight / 25,
            titleHeight: res.windowWidth / 750 * 132,
           // lists: cityData.list
          })
        }
      })
   // }, 10)
    that.initBrand();
  },
  inputTyping(e) {
    this.setData({
      inputVal: e.detail.value
    }, () => {
      //输入即搜索
      //this.searchCity()
    })
  },
  clearInput() {
    this.setData({
      inputVal: "",
      inputShowed: false,
      searchResult: []
    })
    wx.hideKeyboard() //强行隐藏键盘
  },
  // 搜索
  searchCity() {
    if (!this.data.inputVal) {
      wx.showToast({
        title: '请输入搜索关键词',
        icon: "none"
      })
      return
    }
    let result = []
    this.data.lists.forEach((item1, index1) => {
      item1.data.forEach((item2, index2) => {
        if (item2.name.indexOf(this.data.inputVal.toLocaleUpperCase()) !== -1) {
          result.push({
            name: item2.name,
            id: item2.id
          })
        }
      })
    })
    this.setData({
      inputShowed: true,
      searchResult: result
    })
  },
  initBrand:function(){
    var that = this;
    xrsdk.request(app,{
      url: config.service.getBrandList,
      login: false,
      success: function (res) {
        if (res.data.code == 1) {
          that.setData({
            lists: res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        }
      },
      fail: function (res) {},
    })
  },
  touchStart(e) {
    this.setData({
      touchmove: true
    })
    let pageY = e.touches[0].pageY - this.data.scrollTop;
    let index = Math.floor((pageY - this.data.titleHeight) / this.data.indexBarItemHeight)
    let item = this.data.lists[index]
    if (item) {
      this.setData({
        scrollViewId: item.letter,
        touchmoveIndex: index
      })
      wx.pageScrollTo({
        scrollTop: this.data.lists[index].stickyTop,
        duration: 0
      })
    }
  },
  touchMove(e) {
    let pageY = e.touches[0].pageY - this.data.scrollTop;
    let index = Math.floor((pageY - this.data.titleHeight) / this.data.indexBarItemHeight)
    let item = this.data.lists[index]
    if (item) {
      this.setData({
        scrollViewId: item.letter,
        touchmoveIndex: index
      })
      wx.pageScrollTo({
        scrollTop: this.data.lists[index].stickyTop,
        duration: 0
      })
    }
  },
  touchEnd() {
    this.setData({
      touchmove: false,
      touchmoveIndex: -1
    })
  },
  touchCancel() {
    this.setData({
      touchmove: false,
      touchmoveIndex: -1
    })
  },
  stickyChange: function (e) {
    let index = e.detail.index;
    let key = `lists[${index}].stickyTop`
    this.setData({
      [key]: e.detail.top
    })
  },
  detail: function (e) {
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面
    prevPage.initCarType(e.currentTarget.dataset.id);
    prevPage.setData({
      'brand_id': e.currentTarget.dataset.id,
      'brand_name': e.currentTarget.dataset.name
    }, function () {
      wx.navigateBack({
        delta: 1
      });
    });
    console.log(e);
  },
  //页面滚动执行方式
  onPageScroll(e) {
    this.setData({
      scrollTop: e.scrollTop
    })
  }
})